package com.lzx.iteam.net;


import android.content.Context;
import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.aidl.ChatMoreMsg;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONException;
import org.json.JSONObject;

public class SendGroupMsg extends CcMsgStructure {
    private Message mmCallback;
    private Context mContext;
    private static String TAG = "SendGroupMsg";

    public SendGroupMsg(Message callback,Context context) {
        mmCallback = callback;
        mContext=context;
    }
    
    @Override
    public void onReceive(JSONObject data) {
        Log.d(TAG, "onReceive --> " + data.toString());
        ChatMoreMsg chatMsg=null;
        try {
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            if(jsonObject.has("ids")){
                chatMsg= (ChatMoreMsg)mmCallback.obj;
                chatMsg.setMsgID(jsonObject.getString("ids"));
            }
//            String onLine=jsonObject.getString("online");
//            if("0".equals(onLine)){
//                ChatService.restartChatService(mContext);
//            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (mmCallback != null) {
            mmCallback.arg1 = 0;
           mmCallback.obj = chatMsg;
            mmCallback.sendToTarget();
        }
    }

    @Override
    public void onError(int error, Object ret) {
        Log.d(TAG, "onError : " + error + " ret: " + ret);
        if (mmCallback != null) { 
            mmCallback.arg1 = -1;
            mmCallback.obj = ret;
            mmCallback.sendToTarget();
        }
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if(mmCallback != null){
            mmCallback.arg1 = error;
            if(!StringUtil.isEmpty(errorMsg)){
                mmCallback.obj = errorMsg;
            }else{
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }
    }
}